package com.qing.demo.netty.server;

import cn.hutool.json.JSONUtil;
import com.qing.demo.netty.msg.Request;
import com.qing.demo.netty.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import org.springframework.stereotype.Component;


/**
 * @author caozhipeng
 * @date 2025/7/25
 */
@Component
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("服务端收到："+ JSONUtil.toJsonStr(msg));
        Request request= (Request) msg;

        Response response=new Response();
        response.setResponseId(request.getRequestId());
        response.setParam(request.getResult()+" 请求成功！返回消息处理");
        ctx.writeAndFlush(response);
//      释放
        ReferenceCountUtil.release( msg);
    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}
